package com.example.administrator.myapplication;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class FirstProviderActivity extends AppCompatActivity {
    ContentResolver contentResolver;
    Uri uri = Uri.parse("content://com.he.week7_cp.firstprovider");
    Button insert, delete, update, query;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_provider);
        contentResolver = getContentResolver();
        insert = (Button) findViewById(R.id.insert);
        delete = (Button) findViewById(R.id.delete);
        update = (Button) findViewById(R.id.update);
        query = (Button) findViewById(R.id.query);
        insert.setOnClickListener(onClickListener);
        delete.setOnClickListener(onClickListener);
        update.setOnClickListener(onClickListener);
        query.setOnClickListener(onClickListener);
    }
    private View.OnClickListener onClickListener =
            new View.OnClickListener() {
                public void onClick(View view) {
                    switch (view.getId()) {
                        case R.id.insert:
                            insert(view);
                            break;
                        case R.id.delete:
                            delete(view);
                            break;
                        case R.id.update:
                             update(view);
                            break;
                        case R.id.query:
                            query(view);
                            break;
                    }
                }
            };
    public void insert(View source) {
        ContentValues values = new ContentValues();
        values.put("name", "Zhang San");
        values.put("age", 25);
        Uri newUri = contentResolver.insert(uri, values);
        Toast.makeText(this, "远程 ContentProvide 新插入记录的 Uri 为：" + newUri, Toast.LENGTH_LONG).show();
    }
    public void delete(View source) {
        int count = contentResolver.delete(uri, "age > 120", null);
        Toast.makeText(this, "远程 ContentProvide 删除记录数为：" + count, Toast.LENGTH_LONG).show();
    }
    public void update(View source) {
        ContentValues values = new ContentValues();
        values.put("name", "Zhang Xiao San");
        int count = contentResolver.update(uri, values, "name = ?", new String[]{"zhang san"});
        Toast.makeText(this, "远程 ContentProvide 更新记录数为：" + count, Toast.LENGTH_LONG).show();
    }
    public void query(View source) {
        Cursor cursor = contentResolver.query(uri, new String[]{"name", "age"}, "name LIKE ? and age > 21", new String[]{"zhang"}, "age");
        Toast.makeText(this, "远程 ContentProvide 返回的 Cursor 为：" + cursor, Toast.LENGTH_LONG).show();
    }
}
